package cn.hls.web.controller.biz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Member;
import cn.hls.entity.ShipOrder;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.MemberService;
import cn.hls.service.ShipOrderService;
import cn.hls.until.GenericsUtils;
import cn.hls.web.controller.base.BaseController;

/**
  *@fileName: ShipOrderController.java
  *@package: cn.hls.web.controller.biz
　*@Description:　托运单Controller
　*@author: yinghui.zhang
　*@email: uuhui@163.com
  *@date: 2016年6月10日
 *
 */
@Controller
@RequestMapping("/shipOrder")
public class ShipOrderController extends BaseController<ShipOrder> {
	@Resource(name = "shipOrderService")
	private ShipOrderService shipOrderService;
	@Resource(name="memberService")
	private MemberService memberService;
	
	
	/**
	 * 表单
	 * 
	 * @param id 编号
	 * @param show 是否显示
	 * @param flag 是否为手动添加
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	public ModelAndView info(Long id, boolean show,boolean flag )
			throws Exception {
		ShipOrder entity = null;
		Member driver=null;//驾驶员信息
		Member escort=null;//押运员信息
		if (id != null) {
			entity = shipOrderService.find(id);
			if(entity.getDriverId()!=null){
				driver=memberService.find(entity.getDriverId());
			}
			if(entity.getEscortId()!=null){
				escort=memberService.find(entity.getEscortId());
			}
			
		}
		ModelAndView mv=new ModelAndView();
		mv.addObject("entity", entity)
			.addObject("driver",driver)
			.addObject("escort",escort);
		
		if (show) {
			mv.setViewName("shipOrder/show");
		} else {
			if(flag){
				mv.setViewName("shipOrder/info2");//通过文本框方式添加电子运单信息
			}else{
				mv.setViewName("shipOrder/info");//通过选择方式添加电子运单信息
			}
		}
		return mv;
	}
	/**
	 * 根据车牌号获取车辆最新的电子运单信息
	 * @param cardno 车牌号
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/orderView")
	public ModelAndView orderView(String carNum,String handCarNum)throws Exception{
		ShipOrder entity = null;
		Member driver=null;//驾驶员信息
		Member escort=null;//押运员信息
		if(StringUtils.hasText(carNum)){
			 entity = shipOrderService.getShipOrderBycar(carNum,handCarNum);
			 if(entity!=null){
				if(entity.getDriverId()!=null){
					driver=memberService.find(entity.getDriverId());
				}
				if(entity.getEscortId()!=null){
					escort=memberService.find(entity.getEscortId());
				}
			 }
		}
		return new ModelAndView("shipOrder/order_show")
		.addObject("entity", entity)
		.addObject("driver",driver)
		.addObject("escort",escort); // 查看页面
	}
	
	/**
	 * 承运方视图
	 * @param id 编号
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/carryView")
	public  ModelAndView carryView(Long id)throws Exception{
		ShipOrder entity=null;
		Member driver=null;//驾驶员信息
		Member escort=null;//押运员信息
		if(id!=null){
			entity=shipOrderService.find(id);
			 if(entity!=null){
					if(entity.getDriverId()!=null){
						driver=memberService.find(entity.getDriverId());
					}
					if(entity.getEscortId()!=null){
						escort=memberService.find(entity.getEscortId());
					}
			}
		}
		return new ModelAndView("shipOrder/carry_show")
				.addObject("entity",entity)
				.addObject("driver",driver)
				.addObject("escort",escort);
	}
	
	/**
	 * 托运方视图
	 * @param id 编号
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/shipperView")
	public ModelAndView shipperView(Long id)throws Exception{
		ShipOrder entity=null;
		if(id!=null){
			entity=shipOrderService.find(id);
		}
		return new ModelAndView("shipOrder/shipper_show")
		.addObject("entity",entity);
	}
	
	/**
	 * 收货方视图
	 * @param id 编号
	 * @return ModelAndVIew
	 * @throws Exception
	 */
	@RequestMapping("/receiverView")
	public ModelAndView receiverView(Long id)throws Exception{
		ShipOrder entity=null;
		if(id!=null){
			entity=shipOrderService.find(id);
		}
		return new ModelAndView("shipOrder/receiver_show")
		.addObject("entity",entity);
	}
	

	/**
	 * 添加操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(ShipOrder entity,HttpServletRequest request) throws Exception {
		String loadGoodsTime=request.getParameter("loadGoodsTime");//装货时间
		String unloadGoodsTime=request.getParameter("unloadGoodsTime");//卸货时间
		if(StringUtils.hasText(loadGoodsTime)){//装货时间存在时
			entity.setLoadGoodsTime(GenericsUtils.StringToDate(loadGoodsTime, "yyyy-MM-dd HH:mm:ss"));
		}
		if(StringUtils.hasText(unloadGoodsTime)){//卸货时间存在
			entity.setUnloadGoodsTime(GenericsUtils.StringToDate(unloadGoodsTime, "yyyy-MM-dd HH:mm:ss"));
		}
		if(!shipOrderService.exists(entity)){
			boolean flag = shipOrderService.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		}else{
			msg.setInfo("存在相同的托运单信息!");
			msg.setStatus(201);
		}
		return msg;
	}

	 
	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		shipOrderService.delete(tempIds);
		return msg;
	}

	/**
	 * 修改操作
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Msg update(ShipOrder entity) throws Exception {
		try {
			boolean flag = shipOrderService.update(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setInfo(e.getMessage());
		}
		return msg;
	}

	/**
	 * 分页查询
	 * 
	 * @param entity 查询的托运单对象
	 * @param page 托运单分页对象
	 * @return PageInfo<ShipOrder>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<ShipOrder> list(ShipOrder entity, PageInfo<ShipOrder> page)
			throws Exception {
		return shipOrderService.loadShipOrderInfo(entity, page);
	}
	
	@RequestMapping("/loadShipOrder")
	@ResponseBody
	public List<ShipOrder> loadShipOrder(ShipOrder entity)throws Exception{
		return shipOrderService.loadShipOrderList(entity);
	}
	
	 
	@RequestMapping("/view")
	public ModelAndView view() {
		return new ModelAndView("/shipOrder/list");

	}
  
}
